package com.androidvizlab.bachelor.customcomponents.fragments;

import java.util.ArrayList;

import com.androidvizlab.bachelor.R;
import com.androidvizlab.bachelor.customcomponents.listenersandadapters.CustomListAdapter;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * A fragment used by the ViewResultDataActivity to show the list of CameraGroups or Camera.
 * 
 * @author TheHive
 */
public class ViewResultDataFragment extends Fragment{
	
	//Main views and container
	private View mainview = null; //View to inflate
	private int layoutId = -1; //Resource id of the xml file to be inflated
	
	//ListView
	private ListView list = null; //camera info list to be displayed
	
	//List Adapter
	private ArrayAdapter<Object> listAdapter = null; //a list adapter to manage view in the list

	//Listener
	private OnItemClickListener onItemClick = null; //listens to which item is clicked on the list
	
	//Data and list
	private ArrayList<Object> itemList = null;//list of camera in camera combination group
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//FRAGMENT ARGUMENTS
		getDataArguments();//Arguments passed by the Activity to set the index of data in the list
		
		//INIT COMPONENTS
		initComponents(layoutId);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
			
		//Depending on which id was passed in the argument, different xml file will be inflated 
		
		switch(layoutId)
		{
		
		//CAMERA COMBINATION
		case R.layout.sortedcamcombination:
			//Inflate main view
			mainview = inflater.inflate(R.layout.sortedcamcombination, container, false);
			
			//Instantiate ListView
			list = (ListView) mainview.findViewById(R.id.sortedCamCombosListView);
			
			//ArrayAdapter
			listAdapter = new CustomListAdapter(getActivity(),R.layout.sortedcamcombo_list,itemList);
			
			//Add ArrayAdapter
			list.setAdapter(listAdapter);
			
			//Add OnItemClickListener
			list.setOnItemClickListener(onItemClick);
			
			break;
		
		//CAMERA INFO
		case R.layout.caminfogrp_list:
			
			//Inflate main view
			mainview = inflater.inflate(R.layout.caminfogrp_list, container, false);
			
			//Instantiate ListView
			list = (ListView) mainview.findViewById(R.id.lvCamInfo);
			
			//ArrayAdapter
			listAdapter = new CustomListAdapter(getActivity(),R.layout.caminfo,itemList);
			
			//Add listadapter
			list.setAdapter(listAdapter);
			
			break;
		}
		
		return mainview;
	}
	
	/**
	 * Receives argument(s) passed by the Activity, this determines
	 * which xml to be inflated. 
	 */
	private void getDataArguments()
	{
		Bundle bundle = this.getArguments();
		
		if(bundle != null)
		{
			layoutId = bundle.getInt("layout_id");
		}
	}
	
	/**
	 * Initialises component needed.
	 */
	private void initComponents(int layoutId)
	{
		
	}
	
	//*** UPDATE LIST CONTENT ***//
	
	/**
	 * Adds a new list of contents and updates the ListView.
	 * 
	 * @param newList new list of contents to be added.
	 */
	public void updateListContent(ArrayList<Object> newList)
	{
		listAdapter.clear(); //clears the old content
		 
		//Adds the new list of contents
		
		for(int i = 0;i < newList.size(); i++)
		{
			listAdapter.add(newList.get(i));
			
			listAdapter.notifyDataSetChanged(); //notify that the data has changed to update the view
		}
		
		Log.i("viz_result_data","data received:fragment");	
	}
	
	//*** GETTERS AND SETTERS ***//

	public ListView getList() {
		return list;
	}

	public void setList(ListView list) {
		this.list = list;
	}

	public ArrayAdapter<Object> getListAdapter() {
		return listAdapter;
	}

	public void setListAdapter(ArrayAdapter<Object> listAdapter) {
		this.listAdapter = listAdapter;
	}

	public ArrayList<Object> getItemList() {
		return itemList;
	}

	public void setItemList(ArrayList<Object> itemList) {
		this.itemList = itemList;
	}

	public OnItemClickListener getOnItemClickListener() {
		return onItemClick;
	}

	public void setOnItemClickListener(OnItemClickListener onItemClick) {
		this.onItemClick = onItemClick;
	}
}
